import 'package:flutter/cupertino.dart';
import 'package:flutter_inappwebview/flutter_inappwebview.dart';
import 'package:get/get.dart';

import '../../components/chrome_safari_browser_example.screen.dart';
import '../../components/in_app_webiew_example.screen.dart';
import '../../components/my_webview.dart';
import '../../services/address.dart';
import '../../services/dio_manager.dart';
import 'my_folder_file_model.dart';
import 'package:share_plus/share_plus.dart';

class FileController extends GetxController{


  int page = 1;

  TextEditingController searchTextEditingController = TextEditingController();


  final ChromeSafariBrowser browser = MyChromeSafariBrowser();

  var dataArr = [];
  /// 文档列表
  requestData({var search})async{

    var params = {
      'page':page,
      'limit':10,
      'tab':'',
      'search':search,
      'folder_id':Get.arguments,
    };

    var json = await DioManager().kkRequest(Address.documentPage,params: params,method: 'get');

    MyFolderFileModel model = MyFolderFileModel.fromJson(json);

    if(page == 1){
      dataArr.clear();
    }
    if(model.data!=null){
      dataArr.addAll(model.data!);
    }

    update();
  }


  /// 上传文件
  requestDataWithUpLoader(var folder_id)async{
    var params = {
      'model_name':'documents.document',
      'res_id':'',
      'folder_id':folder_id,
      'field':'',
    };
    var json = await DioManager().kkRequest(Address.upLoaderUrl,params: params,method: 'get');

    Get.to(MyWebView('', json['data']));
   var data = await  Get.to(InAppWebViewExampleScreen(json['data']));
    // openWebView();

   // var data = await  Get.to(ChromeSafariBrowserExampleScreen(json['data']));

   if(data == 'refresh'){
     requestData();
   }

    // Get.to(InAppBrowserExampleScreen());

    return json['data'];
  }

  openWebView()async{
    await browser.open(
      url: WebUri("http://49.234.222.178:9097/index?res_model=documents.document&res_id=0&res_field=&company_id=1&create_uid=2&write_uid=2&access_token=430f89381edac608ea51365d4d7403dc&folder_id=13&name=file&offset=10"),
      //   url: WebUri(widget.urlStr),
        settings: ChromeSafariBrowserSettings(
            shareState: CustomTabsShareState.SHARE_STATE_OFF,
            isSingleInstance: false,
            isTrustedWebActivity: false,
            keepAliveEnabled: true,
            startAnimations: [
              AndroidResource.anim(
                  name: "slide_in_left", defPackage: "android"),
              AndroidResource.anim(
                  name: "slide_out_right", defPackage: "android")
            ],
            exitAnimations: [
              AndroidResource.anim(
                  name: "abc_slide_in_top",
                  defPackage:
                  "com.pichillilorenzo.flutter_inappwebviewexample"),
              AndroidResource.anim(
                  name: "abc_slide_out_top",
                  defPackage:
                  "com.pichillilorenzo.flutter_inappwebviewexample")
            ],
            dismissButtonStyle: DismissButtonStyle.CLOSE,
            presentationStyle:
            ModalPresentationStyle.OVER_FULL_SCREEN));
  }



  /// 分享文件
  requestDataWithShare(var documentId)async{
    var params = {
      'document_id':documentId
    };
    var json = await DioManager().kkRequest(Address.documentShare,bodyParams: params);
    Share.share('${Address.homeHost}${json['data']}');
  }


  /// 删除文件夹或文件
  requestDataWithUnlink({var type = '文件夹',var document_id})async{
    var params = {
      'document_id':document_id,
      'type':type,
    };
    var json = await DioManager().kkRequest(Address.documentFolderUnlink,bodyParams: params);

  }



  onRefresh()async{

    page = 1;

    requestData(search: searchTextEditingController.text);

  }

  onLoad()async{
    page++;
    requestData(search: searchTextEditingController.text);

  }


  @override
  void onInit() {
    // TODO: implement onInit
    super.onInit();

    print('Get.arguments=========${Get.arguments}');

    requestData();
  }
}